<?php

namespace post\modules\user\controllers;

use post\modules\user\models\Fan;
use post\modules\user\models\User;
use post\modules\user\service\FanService;
use Yii;

use yii\web\Controller;
use post\base\BaseController;

class FanController extends BaseController
{


    public function actionFan()
    {

        $request_data=data_validate(['user_id']);
        if ($request_data == "false") {
            return $this->json_error("参数缺失");
        }

        $star_id=User::find()->where(['id'=>$request_data['user_id']])->asArray()->one();
        if ($star_id == null) {
            return $this->json_error("没有这个用户!");
        }

        $user_id=$this->getUserId();
        $isFan=FanService::isFan($request_data['user_id'],$user_id);
//        dd($isFan->toArray());

        if($isFan!=null){
            $isFan->delete();
            return $this->jsonSuccess("取关成功");
        }else{
            if($user_id==$star_id){
                return $this->json_error("别关注自己!");
            }
            $fan=new Fan();
            $fan->fan_id=$user_id;
            $fan->star_id=$star_id;
            if($fan->save()){
                return $this->jsonSuccess("关注成功");
            }else{
                return $this->json_error("关注失败!");
            }

        }

    }

    public function actionIsfan()
    {
        $request_data=data_validate(['user_id']);
        if ($request_data == "false") {
            return $this->json_error("参数缺失");
        }
        $user_id=$this->getUserId();

        if($request_data['user_id']==$user_id){
            return $this->json_error("这是你自己");
        }



        $isFan=FanService::isFan($request_data['user_id'],$user_id);

        if($isFan!=null){
            return $this->jsonSuccess("true");
        }else{
            return $this->jsonSuccess("false");
        }

    }

    public function actionFanGcount()
    {

        $request_data=data_validate(['user_id']);
        if ($request_data == "false") {
            return $this->json_error("参数缺失");
        }
        $fans_count=Fan::find()->where(['star_id'=>$request_data['user_id']])->count();
        $stars_count=Fan::find()->where(['fan_id'=>$request_data['user_id']])->count();

        $data=[];
        $data['fans_count']=$fans_count;
        $data['stars_count']=$stars_count;
        return $this->jsonSuccess($data);
    }




}
